package ch.unisi.inf.viewer;

import javax.swing.BorderFactory;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import ch.unisi.inf.engine.Image;
import javax.swing.JComponent;

 class ThumbnailFactory extends JComponent{

	 /**
	  * @author matthias.eberli@lu.unisi.ch 
	  */
	 
	private static int THUMB_WIDTH = 120;
	private static int THUMB_HEIGHT = 95;
	private Image image;
	private boolean isSelected;
	
	public ThumbnailFactory(final Image image)
	{
		this.image = image;
		isSelected = false;
	}
	
	public void setImage(final Image image)
	{
		if(image != null){
			this.image = image;
			repaint();
		}
	}
	
	public void setSelected(final boolean isSelected)
	{
		this.isSelected = isSelected;
	}
	
	public boolean isSelectede()
	{
		return isSelected;
	}
	
	/**
	 * paints a border around the thumbnail if the 'isSelected' flag is true
	 * deletes it if false
	 */
	public void paintBorder()
	{
		if(isSelected){
			this.setBorder(BorderFactory.createLineBorder(Color.RED));
		}
		else{
			this.setBorder(null);
		}
		repaint();
	}
	
	public Image getImage()
	{
		return image;
	}
	
	public Dimension getPreferredSize()
    {
        return new Dimension(THUMB_WIDTH, THUMB_HEIGHT);
    }
	
	public void paintComponent(Graphics g)
	{
		if(image != null){
			g.clearRect(0, 0, THUMB_WIDTH, THUMB_HEIGHT);
			g.drawImage(image, 0, 0, THUMB_WIDTH, THUMB_HEIGHT, null);
		}
	}
	
	
}
